package com.yidu.logisticssystem.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yidu.logisticssystem.entity.LogisticsMap;
import com.yidu.logisticssystem.entity.LogisticsSite;
import com.yidu.logisticssystem.entity.LogisticsStaff;
import com.yidu.logisticssystem.entity.vo.MapHuangVo;
import com.yidu.logisticssystem.entity.vo.MapVo;
import com.yidu.logisticssystem.entity.vo.SiteStaffVo;
import com.yidu.logisticssystem.entity.vo.SiteVo;
import com.yidu.logisticssystem.service.LogisticsMapService;
import com.yidu.logisticssystem.service.LogisticsSiteService;
import com.yidu.logisticssystem.service.LogisticsStaffService;
import com.yidu.logisticssystem.utils.BaseMessage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 站点(LogisticsSite)表控制层
 *
 * @author 黄翔
 * @since 2021-03-08 16:46:54
 */
@Api(description= "站点接口")
@RestController
@RequestMapping("logisticsSite")
public class LogisticsSiteController {
    /**
     * 服务对象
     */
    @Resource
    private LogisticsSiteService logisticsSiteService;
    @Resource
    private LogisticsMapService logisticsMapService;
    @Resource
    private LogisticsStaffService logisticsStaffService;
    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public SiteVo selectOne(Integer id) {
        return this.logisticsSiteService.queryById(id);
    }

    /**
     * 分页查询所有
     * @param page
     * @param limit
     * @return
     */

    @ApiOperation(value = "查询所有的站点",notes = "返回参数")
    @GetMapping("/findAll")
    public Map<String,Object> findAll(SiteVo vo,int page,int limit){

        //将参数传入分页助手
        PageHelper.startPage(page,limit);

        Map<String,Object> map = new HashMap<>();
        //查询全部，返回集合
        List<SiteVo> list=logisticsSiteService.queryAll(vo);

        PageInfo<SiteVo> pageInfo = new PageInfo<>(list);
        System.out.println(vo.getSiteArea());
        map.put("code",0);
        map.put("msg","");
        map.put("data",pageInfo.getList());//所有数据
        map.put("count",pageInfo.getTotal());//总行数

        return map;
    }



    @ApiOperation(value = "根据员工编号查找地区")
    @GetMapping("selectidmap")
    public BaseMessage selectidmap(HttpServletRequest request) {
        LogisticsStaff staff=(LogisticsStaff) request.getSession().getAttribute("staff");
        List<LogisticsSite> list = logisticsSiteService.selectidmap(staff.getStaffNum());

        return BaseMessage.succes(list);
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id删除",notes = "返回参数")
    @PostMapping("delete/{id}")
    public BaseMessage delete(@PathVariable int id){
        boolean flag=logisticsSiteService.deleteById(id);

        if(flag){
            return BaseMessage.succes();
        }else{
            return BaseMessage.fail();
        }
    }

    /**
     * 新增和修改
     * @param vo,three_
     * @return
     */
    @ApiOperation(value = "修改和添加功能实现",notes = "返回参数")
    @PostMapping("addOrUpdate")
    //three_为三级联动的第三级
    public BaseMessage addAndUpdate(SiteVo vo,String three_,String siteBoss,String staffName){
        BaseMessage baseMessage = new BaseMessage();
        if(vo.getSiteId()==null){
            //将第三级的值传入vo中
            vo.setSiteArea(three_);
            //将负责人选择框的值传入vo中
            vo.setSiteBoss(siteBoss);
            logisticsSiteService.insert(vo);
            baseMessage.setState(1);
            baseMessage.setMessage("增加成功");
        }else{
            /**
             * 修改数据
             */
            System.out.println(staffName);
            vo.setSiteBoss(staffName);
            logisticsSiteService.update(vo);
            baseMessage.setState(1);
            baseMessage.setMessage("修改成功");
        }
        return baseMessage;
    }


    /**
     * 新增和修改
     * @param siteId
     * @return
     */
    @ApiOperation(value = "显示",notes = "返回参数")
    @PostMapping("showUpdate")
    public Map<String,Object> showUpdate(Integer siteId){
        String str="";
        System.out.println(siteId);
        /**
         * 将id传入impl中
         */
        SiteVo siteVo = logisticsSiteService.queryById(siteId);
        List<SiteStaffVo> list=logisticsStaffService.queryByStaffNum(siteVo.getSiteBoss());
        for(SiteStaffVo s:list){
            siteVo.setSiteBoss(s.getStaffName());
        }
        Map<String,Object> map = new HashMap<>();
        map.put("state",1);
        map.put("msg","");
        map.put("data",siteVo);//所有数据
        map.put("count","");//总行数

        return map;
    }


    /**
     * 新增和修改
     * @param
     * @return
     */
    @ApiOperation(value = "三级联动传值",notes = "返回参数")
    @PostMapping("three")
    public List<MapHuangVo> showSelect(String mapId){
            List<MapHuangVo> list=logisticsMapService.selectzifind1(mapId);
            for (MapHuangVo s:list){
                System.out.println("调出的地区主键"+s.getMapId()+"调出的地区名字："+s.getMapName());
            }
            return list;
    }

    @ApiOperation(value = "站点负责人传值",notes ="返回参数")
    @PostMapping("siteBoss")
    public List<SiteStaffVo> showBoss(){
        System.out.println("到达siteBoss");
        List<SiteStaffVo> list=logisticsStaffService.queryByPostId(15);
        return list;
    }
}
